package com.cheer.listener;

import com.cheer.entity.MallProductBookStoreEntity;
import com.cheer.listener.delay.ProductBookStockDelay;
import com.cheer.service.MallCategoryProductRelationService;
import com.cheer.service.MallProductBookStoreService;
import com.cheer.service.MallProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.DelayQueue;

/**
 * @author chufangzheng
 */
@Component
@Slf4j
public class ProductListener implements ApplicationListener<ContextRefreshedEvent> {

    private static DelayQueue<ProductBookStockDelay> delayQueue = new DelayQueue();

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private MallProductBookStoreService mallProductBookStoreService;

    @Autowired
    private MallCategoryProductRelationService mallCategoryProductRelationService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        // 加载全部库存信息
        List<MallProductBookStoreEntity> list = mallProductBookStoreService.list();
        list.forEach(( productBookStore )-> {
            Date bookDate = productBookStore.getBookDate();
            ProductBookStockDelay productBookStockDelay = new ProductBookStockDelay();
            productBookStockDelay.setMallProductService(mallProductService)
                                 .setMallProductBookStoreService(mallProductBookStoreService)
                                 .setMallCategoryProductRelationService(mallCategoryProductRelationService)
                                 .setBookStoreId(productBookStore.getId())
                                 .setSecond(bookDate.getTime()/1000);
            delayQueue.add(productBookStockDelay);
        });

        new Thread(()->{
            while(true){
                try {
                    ProductBookStockDelay take = delayQueue.take();
                    threadPoolTaskExecutor.execute(take);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }


    public void addQueue( MallProductBookStoreEntity mallProductBookStoreEntity){

        ProductBookStockDelay productBookStock = new ProductBookStockDelay();
        productBookStock.setSecond((mallProductBookStoreEntity.getBookDate().getTime()/1000));
        productBookStock.setBookStoreId(mallProductBookStoreEntity.getId());
        productBookStock.setMallProductBookStoreService(mallProductBookStoreService);
        productBookStock.setMallProductService(mallProductService);
        productBookStock.setMallCategoryProductRelationService(mallCategoryProductRelationService);
        // 添加之前的信息
        delayQueue.add(productBookStock);
        Arrays.stream(delayQueue.stream().toArray()).forEach( productStock->{
            log.info( "队列信息：" + productStock.toString());
        });
    }


}
